package com.car.dao.impl;

import com.car.dao.AdminCarDao;
import com.car.domain.Admin;
import com.car.domain.Car;
import com.car.utils.JDBCUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AdminCarDaoImpl implements AdminCarDao {
    JdbcTemplate jt=new JdbcTemplate(JDBCUtil.getDs());
    @Override
    public List<Car> inquireMyCar(Admin admin) {
        String sql="select * from tab_car where lid=?";

        List<Car> query = jt.query(sql, new BeanPropertyRowMapper<Car>(Car.class), admin.getLid());
        return query;
    }

    @Override
    public List<Car> inquireAllCar() {
        String sql="select * from tab_car";
        List<Car> query = jt.query(sql, new BeanPropertyRowMapper<Car>(Car.class));
        return query;
    }

    @Override
    public int AddCarServer(Car car, String s) {
        String sql="insert into tab_car values(?,?,?,?,?,?)";
        int update = jt.update(sql, s, car.getCname(), car.getCtype(), car.getCprice()
                , car.getCadmin(), car.getLid());
        return update;
    }

    @Override
    public int deleteCar(String s) {
        String sql="delete from tab_car where cid=?";
        int update = jt.update(sql, s);
        return update;
    }

    @Override
    public Car AmendCarServer(String s) {
        String sql="select * from tab_car where cid=?";
        try {
            Car car = jt.queryForObject(sql,
                    new BeanPropertyRowMapper<>(Car.class), s);
            return car;
        } catch (DataAccessException e) {
            return null;
        }

    }

    @Override
    public int AmendCarServerEnd(Car car) {
        String sql="update tab_car set cname=?,ctype=?,cprice=? where cid=?";
        int update = jt.update(sql, car.getCname(), car.getCtype(), car.getCprice(), car.getCid());
        return update;
    }
}
